package com.itc.bi.controller;

import com.itc.bi.dto.*;
import com.itc.bi.entity.DataProcessEntity;
import com.itc.bi.service.DataProcessInfoService;
import com.itc.bi.service.DataProcessService;
import com.itc.bi.vo.ApiConfigVO;
import com.itc.bi.vo.ProcessDataVO;
import com.itc.common.core.domain.R;
import com.itc.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * @author : xuzy
 */
@RestController
@Api(value = "数据加工",tags = "数据加工")
@RequestMapping("/dataProcess")
public class DataProcessController {

    @Autowired
    private DataProcessService service;
    @Autowired
    private DataProcessInfoService infoService;
    @Autowired
    RedissonClient redissonClient;

    @PostMapping("/updatePageSize")
    @ApiOperation(value = "新增修改")
    public R<Boolean> updatePageSize(@RequestBody DataProcessUpdatePageSizeDTO dto){
        return R.ok(service.updatePageSize(dto.getDpId(),dto.getPageSize()));
    }

    @GetMapping("/getApiConfig/{dpId}")
    @ApiOperation("获取接口配置")
    public R<ApiConfigVO> getApiConfig(@PathVariable("dpId") Long dpId){
        return R.ok(service.getApiConfig(dpId));
    }

    @GetMapping("/getId/{dtId}")
    @ApiOperation(value = "根据数据集ID获取数据加工ID")
    public R<DataProcessInitDto> get(@PathVariable("dtId") Long dtId){
        return R.ok(service.getId(dtId));
    }

    @GetMapping("/get/{dpId}")
    @ApiOperation(value = "根据数据加工ID获取加工详情")
    public R<DataProcessDto> getDeTail(@PathVariable("dpId") Long dpId){
        return R.ok(service.get(dpId));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增修改")
    public R save(@RequestBody DataProcessDto dto){
        RLock lock = redissonClient.getLock("process:" + dto.getDpId());
        if(!lock.tryLock()){
            return R.fail("请勿重复提交");
        }
        try {
            service.saveProcess(dto);
            return R.ok();
        }finally {
            lock.unlock();
        }
    }

    @GetMapping("/cancel/{id}")
    @ApiOperation(value = "取消")
    public R cancel(@PathVariable("id") Long id){
        service.cancel(id);
        return R.ok();
    }

    @PostMapping("/exec")
    @ApiOperation(value = "执行")
    public R exec(@Valid @RequestBody DataProcessInfoDto dto){
        return R.ok(infoService.exec(dto));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public R delete(@PathVariable("id") Long id){
        return R.ok(service.removeById(id));
    }

    @PostMapping("/modifyName")
    @ApiOperation(value = "重命名")
    public R modifyName(@RequestBody DataProcessEntity entity){
        service.modifyName(entity);
        return R.ok();
    }

    @PostMapping("/saveInfo")
    @ApiOperation(value = "新增详情")
    public R saveInfo(@Valid @RequestBody DataProcessInfoDto dto){
        RLock lock = redissonClient.getLock("process:" + dto.getDpId());
        if(!lock.tryLock()){
            return R.fail("请勿重复提交");
        }
        try {
            return R.ok(infoService.saveInfo(dto));
        }finally {
            lock.unlock();
        }
    }

    @DeleteMapping("/deleteInfo/{id}")
    @ApiOperation(value = "删除详情")
    public R deleteInfo(@PathVariable("id") Long id){
        infoService.removeById(id);
        return R.ok();
    }

    @PostMapping("/savePolymeric")
    @ApiOperation(value = "聚合指标")
    public R savePolymeric(@Valid @RequestBody PolymericDto dto){
        RLock lock = redissonClient.getLock("polymeric" + SecurityUtils.getUserId() + dto.getDtId());
        if(!lock.tryLock()){
            return R.fail("请勿重复提交");
        }
        try {
            service.savePolymeric(dto);
            return R.ok();
        }finally {
            lock.unlock();
        }
    }

    @GetMapping("/listByDataSetId/{dtId}")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "dtId",value = "数据集ID",dataType = "Long")
    )
    @ApiOperation(value = "数据集加工列表")
    public R<DataProcessListDto> listByDataSetId(@PathVariable("dtId") Long dtId){
        return R.ok(service.listByDataSetId(dtId));
    }

    @GetMapping("/polymeric/{id}")
    @ApiOperation(value = "获取聚合指标详情")
    public R<PolymericDto> polymeric(@PathVariable("id") Long id){
        return R.ok(service.polymericInfoById(id));
    }

    @PostMapping("/expressionsCheck")
    @ApiOperation(value = "公式检验")
    public R expressionsCheck(@RequestBody DataProcessExpressionsCheck check){
        infoService.expressionsCheck(check);
        return R.ok("公式合规");
    }

    @PostMapping("/getProcessDataList")
    @ApiOperation(value = "获取数据集加工数据列表")
    public R<List<ProcessDataVO>> getProcessDataList(@RequestBody ProcessDataDTO dto){
        return R.ok(service.getProcessDataList(dto.getDtId()));
    }

}
